<h4>Single-period Return</h4>
<p>
  The single-period rate of return can be calculated as following:
</p>
\[r = \frac{p_t}{p_0} - 1 = \frac{p_t - p_0}{p_0}\]
<p>
  Where \(r\) is the rate of return, \(p_t\) is the asset price at time \(t\), and \(p_0\) is the asset price at time 0.
</p>

<div class="section-example-container">

<pre class="python">
import numpy as np
rate_return = 102.0/100 - 1
print rate_return
[out]: 0.02
</pre>
</div>
<p>
  Let's say we bought a stock at $100, and half a year later it will grow to $102. A year later the price will come to $104. How to calculate our total return? Well, we can either deem it as a single-period:
</p>
\[r = 104/100 - 1 = 0.04\]
<p>
  or as a two-stage period:
</p>
\[ r = (1+r_1)*(1+r_2) - 1 = \frac{102}{100} * \frac{104}{102} -1 = 0.04\]
<p>
  Here we make calculations twice a year. It's called semi-annual compounding. How about quarterly compounding? Let's assume the stock prices at the end of each quarter are \(p_1, p_2, p_3, p_4\) respectively.
</p>
\[r = (1+r_1)*(1+r_2)*(1+r_3)*(1+r_4) -1\]
<p>
  The rate of return we calculate here is called <strong>cumulative return</strong> or<strong> overall return</strong>. It measures the total return of this asset over a period of time.
</p>
<p>
  Now consider the following situation: we have two strategies: strategy A and strategy B. We backtested strategy A for 1 years and the cumulative return is 20%, while we backtested strategy B for 3 months(one quarter) and the cumulative return is 6%. Which strategy has a high rate of return? Our commonly used method is to convert all the returns into <strong>compounding annual return</strong>, regardless of the investing horizon of each strategy. We can compare the returns of strategies with different time horizon now. Since there are four quarters in a year,the annual return of strategy B is
</p>
\[(1+0.06)^4 = 1+r\]
\[ r = 0.262\]
<p>
  Strategy B has an higher compounding annual return when we compare 26% with 20%.
</p>
<h4>Logarithm Return</h4>
<p>
  In the above example, strategy A has 6% return over three months. Nominally, the annual return would be 4*6% = 24%.
  This nominal annual interest rate is called the stated annual interest rate.
  It is calculated as the periodic interest rate times the number of periods per year. It works according to the simple interest and does not take into account the compounding periods, while the effective annual interest rate is 26% as we calculated above and it does account for intra-year compounding.
  The effective annual interest rate is an essential tool that allows the evaluation of the real return on investment.
  If we assume the number of compounding periods in one year is n, the formula to convert the stated annual interest rate to the effective annual interest rate is
</p>
\[r_{effective}=(1+\frac{r_{nominal}}{n})^n-1\]
<p>
  Now imagine the price of asset is changing every second or even every millisecond, the period of compounding n approaches infinite. This is called <strong>continuous compounding</strong>. The calculation formula is given below:
</p>
\[\lim_{n \to \infty }(1+\frac{r}{n})^n = e^r\]
<p>
  From the above limitation equation, we know that if we assume continuous compounding:
</p>
\[e^{r_{nominal}} = 1 + r_{effective} = \frac{p_t}{p_0}\]
<p>
  Then we take \(ln\) on both side of the equation:
</p>
\[r_{nominal} = ln\frac{p_t}{p_0} = lnp_t - lnp_0\]
<p>
  Here we got the<strong> logarithmic return, </strong>or <strong>continuously compounded return</strong>. This return is the nominal return with the interest compounding every millisecond. To see how it is close to effective interest rate, recall the equation above:
</p>
\[e^{r_{nominal}} = 1 + r_{effective}\]
<p>then we have</p>
\[r_{effective} = e^{r_{nominal}} - 1 \approx r_{nominal}\]
<p>where the second equality holds due to Taylor Expansion and the interest rate being small. This is frequently used when calculating returns, because once we take the logarithm of asset prices, we can calculate the logarithm return by simply doing a subtraction. Here we use Apple stock prices as an example:
</p>

<div class="section-example-container">

<pre class="python">import quandl
import numpy as np
import quandl
quandl.ApiConfig.api_key = 'zNXvSaz2oX5afVGKjf6o'
#get quandl data
aapl_table = quandl.get('WIKI/AAPL')
aapl = aapl_table.loc['2017-3',['Open','Close']]
#take log return
aapl['log_price'] = np.log(aapl.Close)
aapl['log_return'] = aapl['log_price'].diff()
print aapl
</pre>
</div>
<p>
  The output is:
</p>

<div class="section-example-container">

<pre class="python">
Date          Open   Close  log_price  log_return
2017-03-01  137.890  139.79   4.940141         NaN
2017-03-02  140.000  138.96   4.934186   -0.005955
2017-03-03  138.780  139.78   4.940070    0.005884
2017-03-06  139.365  139.34   4.936917   -0.003153
2017-03-07  139.060  139.52   4.938208    0.001291
2017-03-08  138.950  139.00   4.934474   -0.003734
2017-03-09  138.740  138.68   4.932169   -0.002305
2017-03-10  139.250  139.14   4.935481    0.003311
2017-03-13  138.850  139.20   4.935912    0.000431
2017-03-14  139.300  138.99   4.934402   -0.001510
2017-03-15  139.410  140.46   4.944923    0.010521
2017-03-16  140.720  140.69   4.946559    0.001636
2017-03-17  141.000  139.99   4.941571   -0.004988
2017-03-20  140.400  141.46   4.952017    0.010446
2017-03-21  142.110  139.84   4.940499   -0.011518
2017-03-22  139.845  141.42   4.951734    0.011235
2017-03-23  141.260  140.92   4.948192   -0.003542
2017-03-24  141.500  140.64   4.946203   -0.001989
2017-03-27  139.390  140.88   4.947908    0.001705
2017-03-28  140.910  143.80   4.968423    0.020515
2017-03-29  143.680  144.12   4.970646    0.002223
2017-03-30  144.190  143.93   4.969327   -0.001319
2017-03-31  143.720  143.66   4.967449   -0.001878
</pre>
</div>

<p>
  Here we calculated the daily logarithmic return of Apple stock. Given that we know the daily logarithm return of in this month, we can calculate the monthly return by simply sum all the daily returns up.
</p>

<div class="section-example-container">

<pre class="python">month_return = aapl.log_return.sum()
print month_return
[out]: 0.0273081001636
</pre>
</div>
<p>
  It may sounds incorrect to sum up the daily returns, but we can prove that it's mathematically correct. Let's assume the stock prices in a period of time are represented by \([p_0, p_1, p_2, p_3.....p_n]\). Then the cumulative rate of return is given by:
</p>
\[1 + r_{effective} \approx 1+r_{nominal} = ln\frac{p_t}{p_0} = ln\frac{p_t}{p_{t-1}} + ln\frac{p_{t-1}}{p_{t-2}}+......+ln\frac{p_1}{p_0}\]
<p>
  According to the equation above, we can simple sum up each logarithmic return in a period to get the cumulative return. The convenience of this method is also one of the reasons why we use logarithmic return in quantitative finance.
</p>
